local dx_json = require("dx_json")
local log = require("dx_logger")
local dx_sqlite3 = require("dx_sqlite3")

local t_passrecord = {}
-- 查询通行记录
function t_passrecord.dx_sqlite_selectsingle_passrecord(param)
    local result = dx_sqlite3.query(sqlite_client, "SELECT * FROM passrecords where id=" .. param.id)
    local row
    for key, value in pairs(result) do
        row = value
        break
    end
    return true, row
end

-- 新增通行记录
function t_passrecord.dx_sqlite_save_passrecord(data)
    data.extra = dx_json.encode(data.extra)
    dx_sqlite3.execute(
        sqlite_client,
        "INSERT INTO passrecords ('id', 'code','type','time','result','extra') values('" ..
            data.id ..
                "','" ..
                    data.code ..
                        "'," .. data.type .. "," .. data.time .. "," .. data.result .. ",'" .. data.extra .. "')"
    )
    return true
end

-- 删除通行记录
function t_passrecord.dx_sqlite_del_passrecord(param)
    local area = ""
    for i, v in ipairs(param) do
        area = area .. "'" .. v .. "'"
        if #param ~= i then
            area = area .. ","
        end
    end
    dx_sqlite3.execute(sqlite_client, "delete from passrecords where id in (" .. area .. ")")

    return true
end

-- 查询通行记录总条数
function t_passrecord.dx_sqlite_query_passrecord_count()
    local count = 0
    local result = dx_sqlite3.query(sqlite_client, "SELECT COUNT(*) as count FROM passrecords")
    for key, value in pairs(result) do
        count = value.count
        break
    end
    return true, count
end

return t_passrecord
